<template>
  <div class="login">
    <div class="video-background">
      <video
        class="video"
        autoplay
        muted
        loop
        src="https://mokr.oss-cn-hangzhou.aliyuncs.com/login/welcome-video.mp4"
        poster="https://mokr.oss-cn-hangzhou.aliyuncs.com/login/welcome-video-poster.png"
      />
    </div>
    <div class="login-card">
      <form class="page-main">
        <h4>用户登录</h4>
        <div class="form-input margin-top-bottom">
          <input type="text" maxlength="16" v-model="loginForm.username" placeholder="4到16位用户名" class="tel" />
        </div>
        <div class="form-input">
          <input type="password" maxlength="16" v-model="loginForm.password" placeholder="请输入密码" />
        </div>
        <div class="confirm">
          <span class="color-grey">登录即同意</span>
          <a target="_blank" href="//test.mediportal.com.cn/patientWechat/web/h5/assistant.html" class="color-click">用户协议</a>
          <span class="color-grey">和</span>
          <a target="_blank" href="//test.mediportal.com.cn/patientWechat/web/h5/assistantPrivacy.html" class="color-click">隐私政策</a>
        </div>
        <el-button type="danger" style="width: 100%" :loading="loading" @click.prevent="login">登录</el-button>
      </form>
    </div>
  </div>
</template>

<script>
import { loginAndRegister } from './layout/api';
import md5 from 'md5';
import { setToken } from '@/utils/auth';
import store from '../store/index';

export default {
  data() {
    return {
      name: '登录',
      loading: false,
      loginForm: {
        username: '',
        password: ''
      }
    };
  },
  methods: {
    login() {
      let username = this.loginForm.username.trim(),
        psd = this.loginForm.password.trim();

      if (!username || username.length > 16 || username.length < 4) {
        this.$message.error('请输入4到16位长度的用户名');
        return;
      }
      if (!psd || psd.length > 16 || psd.length < 6) {
        this.$message.error('请输入6到16位长度的用户密码');
        return;
      }
      this.loading = true;
      loginAndRegister({
        username,
        password: md5(username + 'okr' + psd)
      }).then(res => {
        this.loading = false;
        if (res.success) {
          setToken(res.data.token);
          store.commit('SET_VALUE_BY_KEY', {
            token: res.data.token,
            userId: res.data.user.userId,
            userName: username
          });
          this.$router.push('/profile');
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.login {
  position: relative;
  width: 100%;
  height: 100vh;
  min-width: 1024px;
  .video-background {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 0;
    .video {
      width: 100%;
      height: 100%;
      object-fit: cover;
      object-position: left;
    }
  }
  .login-card {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 460px;
    transform: translate(-50%, -50%);
    z-index: 1;
    .page-main {
      padding: 40px 68px;
      background: #fff;
      border-radius: 3px;
      h4 {
        line-height: 28px;
        vertical-align: top;
        font-size: 20px;
        color: #222;
        display: inline-block;
        margin-right: 30px;
        padding-bottom: 8px;
        font-weight: 500;
      }
      .form-input {
        position: relative;
        width: 100%;
        height: 40px;
        line-height: 22px;
        padding: 8px 10px;
        border-radius: 4px;
        border: solid 1px #d9d9d9;
        input {
          border: none;
          height: 22px;
          line-height: 22px;
          outline: none;
          box-shadow: none;
        }
        .tel {
          width: 100%;
        }
      }
      .margin-top-bottom {
        margin: 26px 0 12px 0;
      }
      .confirm {
        font-size: 12px;
        line-height: 20px;
        margin: 18px 0 8px 0;
        .color-grey {
          color: #999;
        }
        .color-click {
          color: #222;
          margin: 0 6px;
          text-decoration: none;
        }
        .color-click:hover {
          color: #458bc4;
        }
      }
    }
  }
}
</style>
